package com.hans.scala.collection


/**
 * 列表/数组 缓存
 * Created by Hans on 2014/4/8.
 */
object SeqExample {
  def main(args: Array[String]) {
    //list buffer
    import scala.collection.mutable
    val buf = new mutable.ListBuffer[Int]
    buf += 1
    buf += 2
    buf.toList
    buf.toArray

    //队列
    //不可变
    import scala.collection.immutable
    val empty = immutable.Queue[Int]()
    val has1 = empty.enqueue(1)
    val has123 = has1.enqueue(List(2, 3))
    val (element, has23) = has123.dequeue
    //可变
    import scala.collection.mutable
    val queue = mutable.Queue[String]()
    queue += "a"
    queue ++= List("b", "c")
    queue.dequeue()
    println(queue)

    //iterate
    val q0 = collection.immutable.Queue("1", "Two", "iii")
    Iterator.iterate(q0) {
      qi =>
        val (e, q) = qi.dequeue
        println("I just dequeued " + e) // Your side-effecting operations go here
        if (e.length != 2) q.enqueue("..") // Your changes to the queue go here
        else q
    }.takeWhile(!_.isEmpty).foreach(identity)

    //stack
    val stack = mutable.Stack[Int]()
    stack push 1
    stack
    stack push 2
    stack
    stack pop()
    stack

  }
}
